﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.Common;
using ZX.DBModule;
using ZX.BLL.Extend;
using System.Text;
using ZX.EHR.Library;
using ZX.CRM.Bussiness;
using ZX.SYS.Library;
using ZX.CRM.Library;

namespace ZX.WebApp.Crm.Customer
{
    public partial class ListPC : ListPage
    {
        public string strId = "00000000-0000-0000-0000-000000000000";
        BTCrmCustomer bll = new BTCrmCustomer();
        public StringBuilder _strHtml = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 权限 
                if (this.UserId == null)
                {
                    Response.Redirect("/Error.aspx?message=1&functionId=" + FunctionID);
                    return;
                }
                if (!CheckFunctionRight())
                {
                    Response.Redirect("/Error.aspx?message=2&functionId=" + FunctionID);
                    return;
                }
                ViewState["pageindex"] = "0";
                bind();
            }
        }
        #region 事件方法
        /// <summary>
        /// 获取私海个数
        /// </summary>
        /// <returns></returns>
        private int getPrivateNum() {
            int iCount = 0;
            if (this.GetStringFromQueryString("treeId", "") == "")
            {
                iCount = bll.GetPrivateNumBySet(this.UserId.ToString());
            }
            else
            {
                iCount = bll.GetPrivateNumBySet(this.GetStringFromQueryString("treeId", ""));
            }
            if (iCount<1)
            {
                iCount = bll.GetPrivateNum("PrivateAreaTotalSet");
            }
            return iCount;
        }

        // 初始化数据源
        protected void bind()
        {
            if (Request.QueryString["treeId"] != null)
            {
                bind(Request.QueryString["treeId"]);
            }
            else
            {
                DatUser du = new DatUser();
                du = Session["User"] as DatUser;
                //if (du != null)
                {
                    IList<V_CrmPrivateCustom> ilist = bll.GetAllCustomerByEmp(getPrivateNum(), du.UserID);
                    //if (ilist != null && ilist.Count > 0)
                    {
                        SourseList(ilist);
                    }
                }
            }
        }
        //重载 初始化数据源
        public void bind(object guid)
        {
            IList<V_CrmPrivateCustom> ilist = bll.GetAllCustomerByEmp(getPrivateNum(), new Guid(CSVHelper.FmSQL(guid)));
            //if (ilist != null && ilist.Count > 0)
            {
                SourseList(ilist);
            }
        }
        //绑定数据集
        private void SourseList(IList<V_CrmPrivateCustom> ilist)
        {

            if (ilist != null && ilist.Count > 0)
            {
                PagedDataSource objPds = new PagedDataSource();
                objPds.DataSource = ilist;

                objPds.AllowPaging = true;
                objPds.PageSize =Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PageNum"]);
                objPds.CurrentPageIndex = int.Parse(ViewState["pageindex"].ToString());
                if ((int.Parse(ViewState["pageindex"].ToString()) == ilist.Count / objPds.PageSize) && ilist.Count % objPds.PageSize == 0 && int.Parse(ViewState["pageindex"].ToString()) > 0)
                {
                    objPds.CurrentPageIndex = (ilist.Count / objPds.PageSize) - 1;
                }
                if (!objPds.IsLastPage)
                {
                    lkNext.Visible = true;
                }
                else
                {
                    lkNext.Visible = false;
                }
                if (!objPds.IsFirstPage)
                {
                    lkPre.Visible = true;
                }
                else
                {
                    lkPre.Visible = false;
                }


                dltList.DataSource = objPds;
                dltList.DataBind();
            }
            else
            {
                lkNext.Visible = false;
                lkPre.Visible = false;
                dltList.DataSource = null;
                dltList.DataBind();
            }
        }
        //共享
        protected void delShare(Guid delCid, DatUser dus)
        {
            BaseBussnies bll = new BaseBussnies();
            BCrmCustomerShare Bshare = new BCrmCustomerShare();

            if (dus == null)
            {
                System.Web.HttpContext.Current.Response.Write("请重新登录");
                Response.Redirect("../../login.aspx");
                return;
            }
            string result = string.Empty;
            result = Bshare.ExecDeleteCrmProc(delCid, dus.UserID);
            if (result != "1")
            {
                System.Web.HttpContext.Current.Response.Write("删除用户共享信息有误!");
                return;
            }
        }
        //分页处理
        protected void IndexChanging(object sender, EventArgs e)
        {
            string strCommand = ((LinkButton)sender).CommandArgument.ToString();

            int pageindex = int.Parse(ViewState["pageindex"].ToString());

            if (strCommand == "pre")
            {
                pageindex = pageindex - 1;
            }
            else
            {
                pageindex = pageindex + 1;
            }
            if (pageindex >= 0)
            {
                ViewState["pageindex"] = pageindex;
            }
            else { ViewState["pageindex"] = 0; }


            bind();
        }
        // 格式化时间
        public string GetStringFormat( object obj)
        {
            return (obj == null||obj.ToString().Length<8) ? "": obj.ToString().Substring(0,obj.ToString().Length-3);
        }
        //报错处理
        protected void Page_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();
            if (ex is HttpRequestValidationException)
            {
                Response.Write("系统正在维护中。。。");
                Server.ClearError(); // 如果不ClearError()这个异常会继续传到Application_Error()。
            }
        }
        //私海转公海
        protected void dltList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                object obj = dltList.DataKeys[e.Item.ItemIndex];//取鼠标当前行产品索引
                CrmPrivateCustom cpc = new CrmPrivateCustom();
                cpc.ID = obj == null ? new Guid("00000000-0000-0000-0000-000000000000") : new Guid(obj.ToString());
                BaseBussnies bli = new BaseBussnies(cpc);
                bool bl = bli.delete();
                if (bl)
                {
                    Label hdnCid = e.Item.FindControl("hdnCid") as Label;
                    Label hdnExplain = e.Item.FindControl("hdnExplain") as Label;
                    Label hdnCuser = e.Item.FindControl("hdnCuser") as Label;
                    DatUser du = new DatUser();
                    du = Session["User"] as DatUser;
                    CrmCustomerOperator cco = new CrmCustomerOperator();
                    cco.AddUser = du.UserID;
                    cco.Explain = "";
                    cco.AddTime = DateTime.Now;
                    cco.OperatorContent = "私海转公海";
                    cco.CustomerID = (hdnCid == null || hdnCid.Text == "") ? new Guid("00000000-0000-0000-0000-000000000000") : new Guid(hdnCid.Text);
                    cco.UserId = (hdnCuser == null || hdnCuser.Text == "") ? new Guid("00000000-0000-0000-0000-000000000000") : new Guid(hdnCuser.Text);
                    bli = new BaseBussnies(cco);
                    bli.Insert();
                    CrmCustomer cct = new CrmCustomer();
                    bli = new BaseBussnies(cct);
                    bli.RunSqlCommand("update CrmCustomer set isPrevate=0 where ID='" + CSVHelper.FmSQL((hdnCid == null || hdnCid.Text == "") ? "00000000-0000-0000-0000-000000000000" : hdnCid.Text) + "'");
                    delShare(cco.CustomerID, du);
                }
                ViewState["pageindex"] = "0";
                bind();
            }
        }
        //刷新
        protected void btnTree_Click(object sender, EventArgs e)
        {
            bind();
        }
        //处理 私海转公海 按钮
        protected void btnTrans_Click(object sender, EventArgs e)
        {
            int num = 0;
            string[] strArr = this.TextBox1.Text.Split(',');
            int i = 0;
            foreach (DataListItem item in dltList.Items)
            {
                if (strArr != null && strArr.Length > i && strArr[i] == "true")
                {
                    try
                    {
                        num++;
                        object obj = dltList.DataKeys[item.ItemIndex];//取鼠标当前行产品索引
                        CrmPrivateCustom cpc = new CrmPrivateCustom();
                        cpc.ID = obj == null ? new Guid("00000000-0000-0000-0000-000000000000") : new Guid(obj.ToString());
                        BaseBussnies bli = new BaseBussnies(cpc);
                        bool bl = bli.delete();
                        if (bl)
                        {
                            Label hdnCid = item.FindControl("hdnCid") as Label;
                            Label hdnExplain = item.FindControl("hdnExplain") as Label;
                            Label hdnCuser = item.FindControl("hdnCuser") as Label;
                            DatUser du = new DatUser();
                            du = Session["User"] as DatUser;
                            CrmCustomerOperator cco = new CrmCustomerOperator();
                            cco.AddUser = du.UserID;
                            cco.Explain = "";
                            cco.AddTime = DateTime.Now;
                            cco.OperatorContent = "私海转公海";
                            cco.CustomerID = (hdnCid == null || hdnCid.Text == "") ? new Guid("00000000-0000-0000-0000-000000000000") : new Guid(hdnCid.Text);
                            cco.UserId = (hdnCuser == null || hdnCuser.Text == "") ? new Guid("00000000-0000-0000-0000-000000000000") : new Guid(hdnCuser.Text);
                            bli = new BaseBussnies(cco);
                            bli.Insert();
                            CrmCustomer cct = new CrmCustomer();
                            bli = new BaseBussnies(cct);
                            bli.RunSqlCommand("update CrmCustomer set isPrevate=0 where ID='" + CSVHelper.FmSQL((hdnCid == null || hdnCid.Text == "") ? "00000000-0000-0000-0000-000000000000" : hdnCid.Text) + "'");
                            delShare(cco.CustomerID, du);
                        }

                    }
                    catch (Exception ee)
                    {
                        Console.WriteLine(ee);
                    }
                }
                i = i + 1;
            }
            if (num > 0)
            {
                ViewState["pageindex"] = "0";
                bind();
            }
        }
        #endregion
    }
}